/*
 * @Author: LVGRAPE
 * @Date: 2023-11-09 11:14:57
 * @LastEditTime: 2023-11-09 11:19:31
 * @LastEditors: LVGRAPE
 * @Description: 
 * @FilePath: \zino_fc_v0\zino\misc\TMR_Configuration.c
 * 要啥没啥，爱咋咋的
 */
#include "TMR_Configuration.h"
// #include "GPIO_Configuration.h"
#include <stdbool.h>
#include "maths.h"
#include "USART_Configuration.h"
#include "pwrCtrl.h"
#include "NVIC_Configuration.h"
#include "ADC_Configuration.h"
#include "fc_error.h"
static TMR_TimerBaseInitType TMRx_baseInitStructure;

// NOTE ZINO V2 定时器分配
/*
 * ZINO V2 定时器分配如下图：
 * TMR1_CH1,TMR1_CH2,TMR1_CH3,TMR1_CH4 用来驱动扩展口的OD
 * TMR2_CH3,TMR_CH4,用来在扩展口输出pwm
 * TMR3_CH1,TMR3_CH2,TMR3_CH3,TMR3_CH4用来驱动主电机的正转
 * TMR4_CH1,TMR4_CH2,TMR4_CH3,TMR4_CH4用来驱动主电机的反转
 * TMR5_CH1,TMR5_CH2,TMR5_CH3,TMR5_CH4用来在扩展口输出pwm
 * TMR9,只有2个通道,空闲
 * TMR10,只有一个通道，空闲
 * TMR11用来控制RLED,BLED,GLED,溢出中断控制IP5306
 * 定时频率 T=SystemCoreClock/div/period
 * */
void TMRx_Init(TMR_Type *TMRx, uint16_t div, uint32_t period)
{
	TMRx_baseInitStructure.TMR_DIV = div;
	TMRx_baseInitStructure.TMR_ClockDivision = TMR_CKD_DIV1;
	TMRx_baseInitStructure.TMR_Period = period;
	TMRx_baseInitStructure.TMR_CounterMode = TMR_CounterDIR_Up;
	// TMR1_baseInitStructure.TMR_RepetitionCounter = 4;
	TMR_TimeBaseInit(TMRx, &TMRx_baseInitStructure);
}



/*
IP5306可识别长按键和短按键操作，不需要按键时PIN5脚悬空。
	按键持续时间长于50ms，但小于2s，即为短按动作，短按会打开电量显示灯和升压输出。
	按键持续时间长于2s，即为长按动作，长按会开启或者关闭照明LED。
	小于50ms的按键动作不会有任何响应。
	在1s内连续两次短按键，会关闭升压输出、电量显示和照明LED。
*/
struct ip5306_pulse_s
{
	bool state;
	uint8_t pulseCount;
	uint16_t pulseWidth; // ms
	uint16_t clock_ms;
	uint16_t clock_int;
	uint16_t clock_pulse;
} ip5306_pulse;

/**
 * @brief  kill/activate 5v_voltage_display
 * @note
 * @param  newState: 1:activate 0:kill
 * @retval None
 */
void IP5306_pulse_generate(bool newState)
{
	if (ip5306_pulse.state == true)
		return;
	if (newState)
	{
		ip5306_pulse.pulseCount = 1;
	}
	else
	{

		ip5306_pulse.pulseCount = 2;
	}
	ip5306_pulse.state = true;
	ip5306_pulse.pulseWidth = 250;
	ip5306_pulse.clock_ms = 0;
	ip5306_pulse.clock_int = 0;
	ip5306_pulse.clock_pulse = 0;
// #ifdef ZINO_LITE
	ip5306_PindMode(GPIO_Mode_OUT_PP);
// #else
// 	ip5306_PindMode(GPIO_Mode_OUT_OD);
// #endif // DEBUG
}

